﻿/*!
* jQuery superCalendar plugin:
*
* version 0.1 2011-03-10
* Requires (1)jQuery v1.4.2 or later (2)fullCalendar v1.4.11 or later (3)jquery.ui.dialog.js (4)jquery.ui.datepicker.js
* Dual licensed under the MIT and GPL Version 2 licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Authors: Kim Seoung Min
* Company : Kongyoung DB
*/

(function ($) {
	$.fn.superCalendar = function (method) {
		if (methods[method]) {
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} else if (typeof method === 'object' || !method) {
			return methods.init.apply(this, arguments);
		} else {
			$.error('Method ' + method + ' does not exist on jQuery.tooltip');
		}
	};

	var this_cal;
	var month_type = ["1월", "2월", "3월", "4월", "5월", "6월", "7월", "8월", "9월", "10월", "11월", "12월"];
	var day_type = ["일", "월", "화", "수", "목", "금", "토"];
	var month_header = 'yyyy년 MMMM';
	var week_header = 'yyyy년 MMMM';
	var day_header = 'yyyy년 MMMM d일, dddd';
	var date_type = "yy-mm-dd";

	var header_text;
	var now_year;
	var now_month;

	var add_div_options_arr = [];
	var add_div_div_arr = [];
	var celebrate_arr;

	var cell_Click_func;
	var event_Click_func;
	var event_Save_func;
	var event_Update_func;
	var event_Drop_func;
	var event_Resize_func;
	var event_Delete_func;
	var modify_Dialog_func;
	var inquiry_Dialog_func;

	var save_event = {
		title: 'notitle'
	};

	var tmp_inquiry_html;
	var tmp_modify_html;
	tmp_modify_html += "<div id='dialog_modify' title='Add Event' style='display: none;'>";
	tmp_modify_html += "<table cellpadding='0' cellspacing='0' style='width: 100%; border-collapse: collapse; border: 1px red solid;'>";
	tmp_modify_html += "<tr>";
	tmp_modify_html += "<td class='modi_head' style='width: 15%;'>Type</td>";
	tmp_modify_html += "<td class='modi_body' style='width: 35%;'>";
	tmp_modify_html += "<select id='modi_type'>";
	tmp_modify_html += "<option value='normal'>Normal</option>";
	tmp_modify_html += "<option value='default'>default</option>";
	tmp_modify_html += "<option value='sales'>Sales</option>";
	tmp_modify_html += "<option value='customer'>Customer</option>";
	tmp_modify_html += "<option value='anniversary'>Anniversary</option>";
	tmp_modify_html += "</select>";
	tmp_modify_html += "</td>";
	tmp_modify_html += "<td class='modi_head' style='width: 15%;'>Importance</td>";
	tmp_modify_html += "<td class='modi_body' style='width: 35%;'>";
	tmp_modify_html += "<select id='modi_importance'>";
	tmp_modify_html += "<option value='hot'>HOT</option>";
	tmp_modify_html += "<option value='important'>Important</option>";
	tmp_modify_html += "<option value='normal' selected>Normal</option>";
	tmp_modify_html += "<option value='soso'>SoSo</option>";
	tmp_modify_html += "<option value='default'>default</option>";
	tmp_modify_html += "</select>";
	tmp_modify_html += "</td>";
	tmp_modify_html += "</tr>";
	tmp_modify_html += "<tr>";
	tmp_modify_html += "<td class='modi_head'>Memo";
	tmp_modify_html += "</td>";
	tmp_modify_html += "<td class='modi_body' colspan='3'><input type='text' id='modi_memo' name='modi_memo' style='width: 98%'/>";
	tmp_modify_html += "</td>";
	tmp_modify_html += "</tr>";
	tmp_modify_html += "<tr>";
	tmp_modify_html += "<td class='modi_head'>Duration";
	tmp_modify_html += "</td>";
	tmp_modify_html += "<td class='modi_body' colspan='3'>";
	tmp_modify_html += "<input type='text' id='modi_start' name='modi_start' style='width: 65px;'/>";
	tmp_modify_html += "<select>";
	tmp_modify_html += "<option value='am'>am</option>";
	tmp_modify_html += "<option value='pm'>pm</option>";
	tmp_modify_html += "</select>";
	tmp_modify_html += "<input type='text' id='modi_start_hr' name='modi_start_hr' style='width: 25px;'/>hr";
	tmp_modify_html += "<input type='text' id='modi_start_min' name='modi_start_min' style='width: 25px;'/>min&nbsp;~&nbsp;";
	tmp_modify_html += "<input type='text' id='modi_end' name='modi_end' style='width: 65px;'/>";
	tmp_modify_html += "<select>";
	tmp_modify_html += "<option value='am'>am</option>";
	tmp_modify_html += "<option value='pm'>pm</option>";
	tmp_modify_html += "</select>";
	tmp_modify_html += "<input type='text' id='modi_end_hr' name='modi_end_hr' style='width: 25px;'/>hr";
	tmp_modify_html += "<input type='text' id='modi_end_min' name='modi_end_min' style='width: 25px;'/>min&nbsp&nbsp&nbsp";
	tmp_modify_html += "<input type='checkbox' id='modi_allDay' name='modi_allDay'/>Allday";
	tmp_modify_html += "</td>";
	tmp_modify_html += "</tr>";
	tmp_modify_html += "<tr>";
	tmp_modify_html += "<td class='modi_head'>Details";
	tmp_modify_html += "</td>";
	tmp_modify_html += "<td class='modi_body' colspan='3'><input type='text' id='modi_details' name='modi_details' style='width: 98%'/>";
	tmp_modify_html += "</td>";
	tmp_modify_html += "</tr>";
	tmp_modify_html += "</table>";
	tmp_modify_html += "</div>";

	tmp_inquiry_html += "<div id='dialog_inquiry' title='Inquiry Event' style='display: none;'>";
	tmp_inquiry_html += "<table cellpadding='0' cellspacing='0' style='width: 100%; border-collapse: collapse; border: 1px red solid;'>";
	tmp_inquiry_html += "<tr>";
	tmp_inquiry_html += "<td class='inquiry_head' style='width: 15%;'>Type</td>";
	tmp_inquiry_html += "<td class='inquiry_body' style='width: 35%;'>";
	tmp_inquiry_html += "<span id='inquiry_type'></span>";
	tmp_inquiry_html += "</td>";
	tmp_inquiry_html += "<td class='inquiry_head' style='width: 15%;'>Importance</td>";
	tmp_inquiry_html += "<td class='inquiry_body' style='width: 35%;'>";
	tmp_inquiry_html += "<span id='inquiry_importance'></span>";
	tmp_inquiry_html += "</td>";
	tmp_inquiry_html += "</tr>";
	tmp_inquiry_html += "<tr>";
	tmp_inquiry_html += "<td class='inquiry_head'>Memo";
	tmp_inquiry_html += "</td>";
	tmp_inquiry_html += "<td class='inquiry_body' colspan='3'><span id='inquiry_memo'></span>";
	tmp_inquiry_html += "</td>";
	tmp_inquiry_html += "</tr>";
	tmp_inquiry_html += "<tr>";
	tmp_inquiry_html += "<td class='inquiry_head'>Duration";
	tmp_inquiry_html += "</td>";
	tmp_inquiry_html += "<td class='inquiry_body' colspan='3'><span id='inquiry_duration'></span>";
	tmp_inquiry_html += "</td>";
	tmp_inquiry_html += "</tr>";
	tmp_inquiry_html += "<tr>";
	tmp_inquiry_html += "<td class='inquiry_head'>Details";
	tmp_inquiry_html += "</td>";
	tmp_inquiry_html += "<td class='inquiry_body' colspan='3'><span id='inquiry_details'></span>";
	tmp_inquiry_html += "</td>";
	tmp_inquiry_html += "</tr>";
	tmp_inquiry_html += "</table>";
	tmp_inquiry_html += "</div>";

	var default_setting = {
		theme: true,
		header: {
			left: 'prevYear,prev,next,nextYear today',
			center: 'title',
			right: 'month,agendaWeek,agendaDay'
		},
		titleFormat: {
			month: month_header,
			week: week_header,
			day: day_header
		},
		weekMode: 'liquid',
		editable: true,
		selectable: true,
		selectHelper: true,
		select: function (startDate, endDate, allDay, jsEvent, view) {
			$(this_cal).superCalendar('cell_Click_Save', startDate, endDate, allDay, jsEvent, view);
		},

		eventClick: function (event, jsEvent, view) {
			$(this_cal).superCalendar('event_Click_Save', event, jsEvent, view);
		},

		eventDrop: function (event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view) {
			$(this_cal).superCalendar('event_Drop_Save', event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view);
		},

		eventResize: function (event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view) {
			$(this_cal).superCalendar('event_Resize_Save', event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view);
		},

		droppable: true,

		drop: function (date, allDay) {
			var eventObject = $(this).data('eventObject');
			$(this_cal).superCalendar('external_Event_Save', date, allDay, eventObject);
		},

		viewDisplay: function (view) {
			var inner_header_title = $('.fc-header-title').html();

			$('.fc-header-title').html("<input id='_tmp_header_input' type='text' value='" + inner_header_title + "' style='text-align: center; border: 0px; disable: true; z-index: 10;' />");

			$('#_tmp_header_input').datepicker({
				selectOtherMonths: true,
				dateFormat: date_type,
				dayNames: day_type,
				dayNamesMin: day_type,
				dayNamesShort: day_type,
				monthNames: month_type,
				monthNamesShort: month_type,
				showButtonPanel: true,
				closeText: 'Cancel',
				showMonthAfterYear: true,
				changeMonth: true,
				changeYear: true,
				onSelect: function (dateText, inst) {
					dateText = stringToMasterDate(dateText);
					var goToYr = dateText.getFullYear();
					var goToMon = dateText.getMonth();
					var goToDay = dateText.getDate();

					this_cal.fullCalendar('gotoDate', goToYr, goToMon, goToDay);
				}
			});

			header_text = view.title;
			$(this_cal).superCalendar('refresh_Super_Calendar');
		},

		eventAfterRender: function (event, element, view) {
			$(this_cal).superCalendar('refresh_Super_Calendar');
		}
	};

	var methods = {
		init: function (client_setting) {
			if (client_setting == null) {
				this_cal = $(this).fullCalendar(default_setting);
			} else {

				month_type = (client_setting.monthNames) ? client_setting.monthNames : month_type;
				day_type = (client_setting.dayNames) ? client_setting.dayNames : day_type;
				date_type = (client_setting.dateType) ? client_setting.dateType : date_type;
				tmp_inquiry_html = (client_setting.inquiry_html) ? client_setting.inquiry_html : tmp_inquiry_html;
				tmp_modify_html = (client_setting.modify_html) ? client_setting.modify_html : tmp_modify_html;
				month_header = (client_setting.month_header) ? client_setting.month_header : month_header;
				week_header = (client_setting.week_header) ? client_setting.week_header : week_header;
				day_header = (client_setting.day_header) ? client_setting.day_header : day_header;

				for (var property in client_setting) {
					var property_value = client_setting[property];
					if (property_value) {
						default_setting[property] = client_setting[property];
					}
				}

				this_cal = $(this).fullCalendar(default_setting);
				//If pamameter 'client_setting' has additional options, this block should apply the options.
			}

			//[start]make tmp_event_object for specific event
			$('body').append("<div id='tmp_event_object' style='display: none;'></div>")
			//[end]make tmp_event_object for specific event

			//[start]get modify html structure
			$('body').append(tmp_modify_html);
			//[end]get modify html structure

			//[start]get inquriy html structure
			$('body').append(tmp_inquiry_html);
			//[end]get inquriy html structure

			//[start]datapicker setting
			$('#modi_start, #modi_end').datepicker({
				showMonthAfterYear: true,
				selectOtherMonths: true,
				dateFormat: date_type,
				dayNames: day_type,
				dayNamesMin: day_type,
				dayNamesShort: day_type,
				monthNames: month_type,
				monthNamesShort: month_type
			});
			//[end]datapicker setting
		},

		init_Tmp_Event: function () {
			save_event = {
				title: 'notitle'
			};

			$("#tmp_event_object").data('eventObject', save_event);
		},

		set_Date: function () {
			$('.exact_date').remove();

			var inner_html = "<span class='_t_date_class exact_date' style='display: none;'>날짜</span>";

			var count = 0;

			if ((typeof header_text).toLowerCase() == 'string') {
				header_text = yearmonthPicker(header_text);
				now_year = header_text[0];
				now_month = header_text[1];
			}

			$(this_cal).find('.fc-day-number').each(function () {
				var td_yr = parseInt(now_year);
				var td_month = parseInt(now_month);
				var td_day = $(this).html();

				var addId;

				if (td_day == "1") count += 1;

				if (count == 0) {
					td_yr = (now_month == 1) ? now_year - 1 : now_year;
					td_month = (now_month == 1) ? 12 : now_month - 1;
				} else if (count == 1) {
					td_yr = now_year;
					td_month = now_month;
				} else if (count == 2) {
					td_yr = (now_month == 12) ? now_year + 1 : now_year;
					td_month = (now_month == 12) ? 1 : now_month + 1;
				}
				addDateId = td_yr + "-" + dayFormatModify(td_month) + "-" + dayFormatModify(td_day);
				$(this).before(inner_html);
				$('._t_date_class').attr('id', addDateId).removeClass('_t_date_class');
			});
		},

		add: function (event_obj) {
			for (var z = 0; z < event_obj.length; z++) {

				if ((typeof event_obj[z].allDay).toLowerCase() == "string") {
					event_obj[z].allDay = (event_obj[z].allDay == "false") ? false : true;
				} else if ((typeof event_obj[0].allDay).toLowerCase() == "boolean") {
					event_obj[z].allDay = (event_obj[z].allDay == false) ? false : true;
				}

				$(this_cal).fullCalendar('renderEvent', event_obj[z], true);
			}
		},

		add_Celebrate: function (celebrate) {
			celebrate_arr = celebrate;

			$(this_cal).superCalendar('show_Celebrate');
		},

		show_Celebrate: function () {
			if ((typeof celebrate_arr).toLowerCase() != "undefined") {
				$(".cele-remove").remove();
				for (var z = 0; z < celebrate_arr.length; z++) {
					celebrate_arr[z].className = ((typeof celebrate_arr[z].className).toLowerCase() == "undefined") ? "happy" : celebrate_arr[z].className;
					$(this_cal).find("#" + celebrate_arr[z].date).before("<span class='cele-remove " + celebrate_arr[z].className + "'>" + celebrate_arr[z].title + "</span>");
				}
			}
		},

		add_Div: function (add_options, add_div) {
			add_div_options_arr.push(add_options);
			add_div_div_arr.push(add_div);

			$(this_cal).superCalendar('show_Div');
		},

		show_Div: function () {
			$('._remove_class').remove();

			var week_num_p = ".fc-week0, .fc-week1, .fc-week2, .fc-week3, .fc-week4, .fc-week5, .fc-week6";
			var week_p = ".fc-sun, .fc-mon, .fc-tue, .fc-wed, .fc-thu, .fc-fri, .fc-sat";
			var weekday_p = ".fc-mon, .fc-tue, .fc-wed, .fc-thu, .fc-fri";
			var weekend_p = ".fc-sun, .fc-sat";
			var sun_p = ".fc-sun";
			var mon_p = ".fc-mon";
			var tue_p = ".fc-tue";
			var wed_p = ".fc-wed";
			var thu_p = ".fc-thu";
			var fri_p = ".fc-fri";
			var sat_p = ".fc-sat";
			var tmp_para = "";

			for (var x = 0; x < add_div_options_arr.length; x++) {
				if ($('div.fc-view.fc-view-month.fc-grid').css('display') != 'none') {
					if (add_div_options_arr[x] != null) {
						if (add_div_options_arr[x] == 'week') tmp_para = week_p;
						else if (add_div_options_arr[x] == 'weekday') tmp_para = weekday_p;
						else if (add_div_options_arr[x] == 'weekend') tmp_para = weekend_p;
						else if (add_div_options_arr[x] == 'sun') tmp_para = sun_p;
						else if (add_div_options_arr[x] == 'mon') tmp_para = mon_p;
						else if (add_div_options_arr[x] == 'tue') tmp_para = tue_p;
						else if (add_div_options_arr[x] == 'wed') tmp_para = wed_p;
						else if (add_div_options_arr[x] == 'thu') tmp_para = thu_p;
						else if (add_div_options_arr[x] == 'fri') tmp_para = fri_p;
						else if (add_div_options_arr[x] == 'sat') tmp_para = sat_p;

						$(this_cal).find(week_num_p).children(tmp_para).each(function () {
							var xx = $(this).offset().left - $('div.fc-view.fc-view-month.fc-grid').offset().left;
							var yy = $(this).offset().top - $('div.fc-view.fc-view-month.fc-grid').offset().top;
							var wid = $(this).width() - 30;
							var inner_html = "<span class='_t_class _remove_class'>" + add_div_div_arr[x] + "</span>";

							$(this).prepend(inner_html);
							$('._t_class').attr('style', "z-index: 1; position: absolute; width: " + wid + "px;" + "top: " + yy + "px;" + "left: " + xx + "px;").removeClass('_t_class');
						});
					}
				}
			}
		},

		external_Event: function (div) {
			$(div).each(function () {
				var eventObject = {
					title: $.trim($(this).text())
				};

				$(this).data('eventObject', eventObject);
				$(this).css('cursor', 'pointer');

				$(this).draggable({
					zIndex: 99999,
					revert: true,
					revertDuration: 300
				});
			});
		},

		external_Event_Save: function (date, allDay, eventObject) {
			var copiedEventObject = $.extend({}, eventObject);

			copiedEventObject.allDay = allDay;
			if (allDay) {
				copiedEventObject.start = dateToShortString(date);
				copiedEventObject.end = dateToShortString(date);
			} else {
				copiedEventObject.start = dateToLongString(date);
				copiedEventObject.end = dateToLongString(date);
			}

			$(this_cal).superCalendar('event_Save_Save', copiedEventObject);
		},

		cell_Click: function (func) {
			if (cell_Click_func == null) {
				cell_Click_func = func;
			}
		},

		cell_Click_Save: function (startDate, endDate, allDay, jsEvent, view) {
			cell_Click_func(startDate, endDate, allDay, jsEvent, view);

			$(this_cal).superCalendar('modify_Dialog_Init', save_event);

			$('#dialog_modify').dialog('open');
			return true;
		},

		event_Click: function (func) {
			if (event_Click_func == null) {
				event_Click_func = func;
			}
		},

		event_Click_Save: function (event, jsEvent, view) {
			if (false) {	//If user click mouse after press 'ctrl' key, save the array events ==> (key_down == 17) && (mouse_down == 1)
			} else {
				event_Click_func(event, jsEvent, view);

				$(this_cal).superCalendar('modify_Dialog_Init', event);
				$(this_cal).superCalendar('inquiry_Dialog_Init', event, jsEvent, view);

				$('#dialog_inquiry').dialog('open');
				return false;
			}
		},

		event_Save: function (func) {
			if (event_Save_func == null) {
				event_Save_func = func;
			}
		},

		event_Save_Save: function (event) {
			event_Save_func(event);
			$(this_cal).fullCalendar('renderEvent', event, true);
		},

		event_Update: function (func) {
			if (event_Update_func == null) {
				event_Update_func = func;
			}
		},

		event_Update_Save: function (event) {
			event_Update_func(event);
			$(this_cal).fullCalendar('updateEvent', event);
		},

		event_Resize: function (func) {
			if (event_Resize_func == null) {
				event_Resize_func = func;
			}
		},

		event_Resize_Save: function (event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view) {
			event_Resize_func(event, dayDelta, minuteDelta, revertFunc, jsEvent, ui, view);
		},

		event_Drop: function (func) {
			if (event_Drop_func == null) {
				event_Drop_func = func;
			}
		},

		event_Drop_Save: function (event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view) {
			event_Drop_func(event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view);
		},

		event_Delete: function (func) {
			if (event_Delete_func == null) {
				event_Delete_func = func;
			}
		},

		event_Delete_Save: function (event) {
			event_Delete_func(event);
			$(this_cal).fullCalendar('removeEvents', event.id);
		},

		modify_Dialog: function (func) {
			modify_Dialog_func = func;
		},

		modify_Dialog_Init: function (event) {
			if (event.id == null || event.id == "") {
				save_event.id = save_event.id;
			} else {
				$("#tmp_event_object").data('eventObject', event);
				save_event = $("#tmp_event_object").data('eventObject')
			}

			modify_Dialog_func(save_event);
		},

		inquiry_Dialog: function (func) {
			inquiry_Dialog_func = func;
		},

		inquiry_Dialog_Init: function (event, jsEvent, view) {
			inquiry_Dialog_func(event, jsEvent, view);
		},

		refresh_Super_Calendar: function () {
			$(this_cal).superCalendar('set_Date');
			$(this_cal).superCalendar('show_Div');
			$(this_cal).superCalendar('show_Celebrate');
			$(this_cal).superCalendar('init_Tmp_Event');
		}
	}
})(jQuery);


function dateToShortString(date) {
	return date.getFullYear() + "-" + dayFormatModify(date.getMonth() + 1) + "-" + dayFormatModify(date.getDate());
}

function dateToLongString(date) {
	return date.getFullYear() + "-" + dayFormatModify(date.getMonth() + 1) + "-" + dayFormatModify(date.getDate()) + " " + dayFormatModify(date.getHours()) + ":" + dayFormatModify(date.getMinutes());
}

function stringToMasterDate(date_str) {
	var arr_1, arr_2_1, arr_2_2;

	arr_1 = date_str.split(" ");

	arr_2_1 = arr_1[0].split("-");
	arr_2_1[1] = dayFormatModify(arr_2_1[1] - 1);
	arr_2_1[2] = dayFormatModify(arr_2_1[2]);

	if (arr_1[1]) {
		arr_2_2 = arr_1[1].split(":");
		arr_2_2[0] = dayFormatModify(arr_2_2[0]);
		arr_2_2[1] = dayFormatModify(arr_2_2[1]);
		return new Date(arr_2_1[0], arr_2_1[1], arr_2_1[2], arr_2_2[0], arr_2_2[1]);
	} else {
		return new Date(arr_2_1[0], arr_2_1[1], arr_2_1[2]);
	}
}

function dayFormatModify(valueDate) {
	var tmp_valueDate = valueDate + "";

	if (valueDate < 10) {
		if (tmp_valueDate.length == 1) valueDate = '0' + valueDate;
	}
	return valueDate;
}

function arrayToSting(arr) {
	return arr.join();
}

function yearmonthPicker(text) {
	var arr1 = text.split("년 ");
	var arr2 = arr1[1].split("월");

	var returnarr = [arr1[0], arr2[0]];

	return returnarr;
}